import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class MenuPanel extends JPanel{
	JButton register=new JButton();
	JButton drop=new JButton();
	JButton logout=new JButton();
	JButton transcript=new JButton();
	JButton account=new JButton();
	JButton studentPlanning=new JButton();
	JButton currents=new JButton();
	JPanel panel=new JPanel();
	Image img;
	static String SID,student_id;
	static String fname="username",lname="last";
	
	public MenuPanel(){
		
		img=new ImageIcon("menu_background.png").getImage();
		final ImageIcon regi=new ImageIcon("register_button.png");
		final ImageIcon log=new ImageIcon("logout_button.png");
		final ImageIcon acc=new ImageIcon("account_button.png");
		final ImageIcon trans=new ImageIcon("transcript_button.png");
		final ImageIcon student=new ImageIcon("student_button.png");
		final ImageIcon current=new ImageIcon("currents_button.png");
		final ImageIcon ccurrent=new ImageIcon("currents_cbutton.png");
		final ImageIcon dro=new ImageIcon("drop_button.png");
		final ImageIcon cregi=new ImageIcon("register_cbutton.png");
		final ImageIcon clog=new ImageIcon("logout_cbutton.png");
		final ImageIcon cacc=new ImageIcon("account_cbutton.png");
		final ImageIcon ctrans=new ImageIcon("transcript_cbutton.png");
		final ImageIcon cstudent=new ImageIcon("student_cbutton.png");
		final ImageIcon cdro=new ImageIcon("drop_cbutton.png");
		
		
		button(register,regi);
		button(logout,log);
		button(transcript,trans);
		button(account,acc);
		button(studentPlanning,student);
		button(currents,current);
		button(drop,dro);
		
		
		
		panel.setPreferredSize(new Dimension(600,400));
		panel.setBackground(Color.white);
		
		
		panel.setOpaque(false);
		panel.setLayout(new GridBagLayout());
		
		setOpaque(false);
		
		String u=LoginPanel.uid;
		
		try{
			String dName = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
			Connection con = DriverManager.getConnection(url);
			java.sql.Statement mysqlStatement = con.createStatement();
			java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT fname, lname, SID, student_id FROM student_list WHERE username='"+u+"'");
			while (resultSet.next()) {
			fname= resultSet.getString(1);
			lname= resultSet.getString(2);
			SID=resultSet.getString(3);
			student_id=resultSet.getString(4);
			}
			con.close();
			}
			catch(Exception ex){}
		
		
		JLabel welcome=new JLabel("Welcome, "+fname+" "+lname+"!");
		JLabel l=new JLabel("");
		
		JPanel p=new JPanel();
		p.setLayout(new GridLayout(9,1));
		p.add(welcome);
		p.add(l);
		p.add(currents);
		p.add(register);
		p.add(drop);
		p.add(transcript);
		p.add(account);
		p.add(studentPlanning);
		p.add(logout);

		p.setOpaque(false);
		
		panel.add(p);
		
		
		
		//panel.setLayout(new BorderLayout());
	
		setLayout(new BorderLayout());
		add(panel,BorderLayout.PAGE_START);
		
		currents.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                remove(panel);
                try {
					add(new CurrentSchedulePanel());
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
                validate();
                repaint();
            }
    });
		currents.setContentAreaFilled(false);
		currents.setIcon(current);
		currents.setRolloverIcon(ccurrent);
		currents.setPressedIcon(ccurrent);
		currents.getModel().addChangeListener(new ChangeListener() {
	        @Override
	        public void stateChanged(ChangeEvent e) {
	            ButtonModel model = (ButtonModel) e.getSource();
	            if (model.isRollover()) {
	                currents.setIcon(ccurrent);
	            } else {
	                currents.setIcon(current);
	            }// etc ...//  if (model.isPressed()) {
	         }
	    });
		transcript.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                remove(panel);
                add(new TranscriptPanel());
                validate();
                repaint();
            }
    });
		transcript.setContentAreaFilled(false);
		transcript.setIcon(trans);
		transcript.setRolloverIcon(ctrans);
		transcript.setPressedIcon(ctrans);
		transcript.getModel().addChangeListener(new ChangeListener() {
	        @Override
	        public void stateChanged(ChangeEvent e) {
	            ButtonModel model = (ButtonModel) e.getSource();
	            if (model.isRollover()) {
	            	transcript.setIcon(ctrans);
	            } else {
	            	transcript.setIcon(trans);
	            }// etc ...//  if (model.isPressed()) {
	         }
	    });
		
		account.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                remove(panel);
                add(new AccountPanel());
                validate();
                repaint();
            }
    });
		account.setContentAreaFilled(false);
		account.setIcon(acc);
		account.setRolloverIcon(cacc);
		account.setPressedIcon(cacc);
		account.getModel().addChangeListener(new ChangeListener() {
	        @Override
	        public void stateChanged(ChangeEvent e) {
	            ButtonModel model = (ButtonModel) e.getSource();
	            if (model.isRollover()) {
	            	account.setIcon(cacc);
	            } else {
	            	account.setIcon(acc);
	            }// etc ...//  if (model.isPressed()) {
	         }
	    });
		
		studentPlanning.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                remove(panel);
                add(new StudentPlanning());
                validate();
                repaint();
            }
    });
		studentPlanning.setContentAreaFilled(false);
		studentPlanning.setIcon(student);
		studentPlanning.setRolloverIcon(cstudent);
		studentPlanning.setPressedIcon(cstudent);
		studentPlanning.getModel().addChangeListener(new ChangeListener() {
	        @Override
	        public void stateChanged(ChangeEvent e) {
	            ButtonModel model = (ButtonModel) e.getSource();
	            if (model.isRollover()) {
	            	studentPlanning.setIcon(cstudent);
	            } else {
	            	studentPlanning.setIcon(student);
	            }// etc ...//  if (model.isPressed()) {
	         }
	    });
		 logout.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                     remove(panel);
                 add(new LoginPanel());
                 validate();
                 repaint();
                 JOptionPane.showMessageDialog(null, "You've successfully logged out");
             }
     });
		 logout.setContentAreaFilled(false);
		 logout.setIcon(log);
		 logout.setRolloverIcon(clog);
		 logout.setPressedIcon(clog);
		 logout.getModel().addChangeListener(new ChangeListener() {
		        @Override
		        public void stateChanged(ChangeEvent e) {
		            ButtonModel model = (ButtonModel) e.getSource();
		            if (model.isRollover()) {
		            	logout.setIcon(clog);
		            } else {
		            	logout.setIcon(log);
		            }// etc ...//  if (model.isPressed()) {
		         }
		    });

    
register.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            
            remove(panel);
            try {
				add(new RegisterPanel());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
            validate();
            repaint();
            
        }
});

register.setContentAreaFilled(false);
register.setIcon(regi);
register.setRolloverIcon(cregi);
register.setPressedIcon(cregi);
register.getModel().addChangeListener(new ChangeListener() {
       @Override
       public void stateChanged(ChangeEvent e) {
           ButtonModel model = (ButtonModel) e.getSource();
           if (model.isRollover()) {
        	   register.setIcon(cregi);
           } else {
        	   register.setIcon(regi);
           }// etc ...//  if (model.isPressed()) {
        }
   });
drop.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            remove(panel);
            try {
				add(new DropPanel());
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
            validate();
            repaint();
        }
});
drop.setContentAreaFilled(false);
drop.setIcon(dro);
drop.setRolloverIcon(cdro);
drop.setPressedIcon(cdro);
drop.getModel().addChangeListener(new ChangeListener() {
       @Override
       public void stateChanged(ChangeEvent e) {
           ButtonModel model = (ButtonModel) e.getSource();
           if (model.isRollover()) {
        	   drop.setIcon(cdro);
           } else {
        	   drop.setIcon(dro);
           }// etc ...//  if (model.isPressed()) {
        }
   });
}
		//We'll set more appropriate layout later ex) gridlayout etc..
	
	@Override
    public void paintComponent(Graphics g) {
		super.paintComponents(g);
        g.drawImage(img, 0, 0, this);
    }
		
	
	private void button(JButton b, ImageIcon i){
		b.setBorderPainted(false);
		b.setBorder(null);
		//button.setFocusable(false);
		b.setMargin(new Insets(0, 0, 0, 0));
		b.setContentAreaFilled(false);
		b.setIcon(i);
		//b.setRolloverIcon(myIcon2);
		//b.setPressedIcon(myIcon3);
	}
	
}
	
	

